/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.tools;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.menu.MenuService;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.user.User;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ivan
 */
public class SessionFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        boolean stop = false;
        if (request.getParameter("action") != null) {
            if (!request.getServletPath().equals("/welcome.do") && !request.getServletPath().equals("/login.do")) {
                if (!request.getServletPath().equals("/login.do")) {
                    HttpSession session = request.getSession(true);
                    if (session.getAttribute(SISConstants.USER ) == null) {
                        session.invalidate();
                        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/welcome.do");
                        requestDispatcher.forward(req, res);
                    } else {
                        try {
                            User user = (User) session.getAttribute(SISConstants.USER );
                            MenuService menuService = (MenuService) CallCtx.getContext(request).getBean(SISBeanConstants.MENU_SERVICE);
                            StringBuffer buff = new StringBuffer();
                            buff.append(request.getServletPath().substring(1) + "?");
                            buff.append("action=" + request.getParameter("action"));
                            boolean valid = menuService.cekByDepartementId(user.getDepartement().getId(), buff.toString());
                            if (session.getAttribute(SISConstants.USER ) == null || session.getAttribute(SISConstants.DEPARTEMENT_ID) == null || !valid) {
                                stop = !valid;
                                session.invalidate();
                                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/welcome.do");
                                requestDispatcher.forward(request, response);
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(SessionFilter.class.getName()).log(Level.SEVERE, null, ex);
                            session.invalidate();
                            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/welcome.do");
                            requestDispatcher.forward(request, response);
                        }
                    }
                }
            }
        }
        if (stop == true) {
        } else {
            chain.doFilter(req, res);
        }
    }

    public void destroy() {
    }
}
